package com.jiyinit.web.core.common.util;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.api.push.model.Message;
import com.jiyinit.web.core.common.config.PropertyPlaceholderConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jiyinit.core.framework.base.util.LogUtil;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.push.model.notification.AndroidNotification;

public class MessagePush {

	final String MASTER_SECRET = "762c618c5bdd41d7b499663b";
	final String APP_KEY = "0ee3e666100ab65cf77c44cc";

	private static final Logger LOGGER = LoggerFactory.getLogger(MessagePush.class);

	/**
	 * 极光推送
	 * @param returnStr
	 * @param userIds
     */
	public MessagePush(Map<String,String> returnStr,String[] userIds){
		String runMode = (String) PropertyPlaceholderConfigurer.getContextProperty("RunMode");//获取运行模式
		if("MASTER".equalsIgnoreCase(runMode)){//生产模式
			JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
			PushPayload payload = buildPushObject_all_alias_alert(returnStr,userIds);
			try {
				PushResult result = jpushClient.sendPush(payload);
				LogUtil.info(LOGGER, "result:{}",result);
			} catch (APIConnectionException e1) {
				LogUtil.info(LOGGER, "e:{}", e1);
			} catch (APIRequestException e) {
				LogUtil.info(LOGGER, "e.getStatus:{}",  e.getStatus());
				LogUtil.info(LOGGER, "e.getErrorCode:{}", e.getErrorCode());
				LogUtil.info(LOGGER, "e.getErrorMessage:{}", e.getErrorMessage());
			}
		}else if("TEST".equalsIgnoreCase(runMode)){//测试模式
			//ios端
			JPushClient iosClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
			PushPayload payload = PushPayload.newBuilder()
					.setPlatform(Platform.ios())
					.setAudience(Audience.alias(userIds))
					.setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder()
							.setAlert(returnStr.get("content"))
							.setContentAvailable(true)
							.setSound("default")
							.addExtras(returnStr)
							.autoBadge()
							.build()).build())
					.setOptions(Options.newBuilder().setApnsProduction(false).build())
					.build();
			try {
				PushResult result = iosClient.sendPush(payload);
				LogUtil.info(LOGGER, "result:{}",result);
			} catch (APIConnectionException e1) {
				LogUtil.info(LOGGER, "e:{}", e1);
			} catch (APIRequestException e) {
				LogUtil.info(LOGGER, "e.getStatus:{}",  e.getStatus());
				LogUtil.info(LOGGER, "e.getErrorCode:{}", e.getErrorCode());
				LogUtil.info(LOGGER, "e.getErrorMessage:{}", e.getErrorMessage());
			}

			//安卓端
			JPushClient androidClient = new JPushClient("6f1848b667b38823870622e4", "89bfe510f1d8bda07f83efef", null, ClientConfig.getInstance());
			PushPayload androidPayload = PushPayload.newBuilder()
					.setPlatform(Platform.android())
					.setAudience(Audience.alias(userIds))
					.setNotification(Notification.newBuilder().addPlatformNotification(AndroidNotification.newBuilder()
							.setAlert(returnStr.get("content"))
							.addExtras(returnStr)
							.build()).build())
					.setOptions(Options.newBuilder().setApnsProduction(true).build())
					.build();
			try {
				PushResult result = androidClient.sendPush(androidPayload);
				LogUtil.info(LOGGER, "result:{}",result);
			} catch (APIConnectionException e1) {
				LogUtil.info(LOGGER, "e:{}", e1);
			} catch (APIRequestException e) {
				LogUtil.info(LOGGER, "e.getStatus:{}",  e.getStatus());
				LogUtil.info(LOGGER, "e.getErrorCode:{}", e.getErrorCode());
				LogUtil.info(LOGGER, "e.getErrorMessage:{}", e.getErrorMessage());
			}
		}
	}

	/**
	 * 极光推送(IOS与安卓同时发送, 用于生产环境)
	 * @param returnStr
	 * @param userIds
     * @return
     */
	public static PushPayload buildPushObject_all_alias_alert(Map<String,String> returnStr,String[] userIds){
		return PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.alias(userIds))
				.setNotification(Notification.newBuilder()
					.addPlatformNotification(IosNotification.newBuilder()
						.setAlert(returnStr.get("content"))
						.setContentAvailable(true)
						.setSound("default")
						.addExtras(returnStr)
						.autoBadge()
						.build())
					.addPlatformNotification(AndroidNotification.newBuilder()
						.setAlert(returnStr.get("content"))
						.addExtras(returnStr)
						.build())
				.build())

				.setOptions(Options.newBuilder().setApnsProduction(true).build())
				.build();
//        public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
//        	return PushPayload.newBuilder()
//        			.setPlatform(Platform.ios())
//        			.setAudience(Audience.tag_and("tag1", "tag_all"))
//        			.setNotification(Notification.newBuilder()
//        					.addPlatformNotification(IosNotification.newBuilder()
//        							.setAlert(ALERT)
//        							.setBadge(5)
//        							.setSound("happy")
//        							.addExtra("from", "JPush")
//        							.build()) .build())
//        							.setMessage(Message.content(MSG_CONTENT))
//        							.setOptions(Options.newBuilder()
//        									.setApnsProduction(true)
//        									.build())
//        									.build();
//        	}http://jpush.github.io/jpush-api-java-client/apidocs/
	}

	/**
	 * 极光推送
	 * @param returnStr
	 * @param userIds
	 * @param andriod
     */
	public MessagePush(Map<String,String> returnStr,String[] userIds,String andriod){
		String runMode = (String) PropertyPlaceholderConfigurer.getContextProperty("RunMode");//获取运行模式
		if("MASTER".equalsIgnoreCase(runMode)){//生产模式
			JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
			PushPayload payload = buildPushObject_all_alias_alert(returnStr,userIds);
			try {
				PushResult result = jpushClient.sendPush(payload);
				LogUtil.info(LOGGER, "result:{}",result);
			} catch (APIConnectionException e1) {
				LogUtil.info(LOGGER, "e:{}", e1);
			} catch (APIRequestException e) {
				LogUtil.info(LOGGER, "e.getStatus:{}",  e.getStatus());
				LogUtil.info(LOGGER, "e.getErrorCode:{}", e.getErrorCode());
				LogUtil.info(LOGGER, "e.getErrorMessage:{}", e.getErrorMessage());
			}
		}else if("TEST".equalsIgnoreCase(runMode)){//测试模式
			//ios端
			JPushClient iosClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
			PushPayload payload = PushPayload.newBuilder()
					.setPlatform(Platform.ios())
					.setAudience(Audience.alias(userIds))
					.setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder()
							.setAlert(returnStr.get("content"))
							.setContentAvailable(true)
							.setSound("default")
							.addExtras(returnStr)
							.autoBadge()
							.build()).build())
					.setOptions(Options.newBuilder().setApnsProduction(false).build())
					.build();
			try {
				PushResult result = iosClient.sendPush(payload);
				LogUtil.info(LOGGER, "result:{}",result);
			} catch (APIConnectionException e1) {
				LogUtil.info(LOGGER, "e:{}", e1);
			} catch (APIRequestException e) {
				LogUtil.info(LOGGER, "e.getStatus:{}",  e.getStatus());
				LogUtil.info(LOGGER, "e.getErrorCode:{}", e.getErrorCode());
				LogUtil.info(LOGGER, "e.getErrorMessage:{}", e.getErrorMessage());
			}

			//安卓端
			JPushClient androidClient = new JPushClient("6f1848b667b38823870622e4", "89bfe510f1d8bda07f83efef", null, ClientConfig.getInstance());
			PushPayload androidPayload = PushPayload.newBuilder()
					.setPlatform(Platform.android())
					.setAudience(Audience.alias(userIds))
					.setNotification(Notification.newBuilder().addPlatformNotification(AndroidNotification.newBuilder()
							.setAlert(returnStr.get("content"))
							.addExtras(returnStr)
							.build()).build())
					.setOptions(Options.newBuilder().setApnsProduction(true).build())
					.build();
			try {
				PushResult result = androidClient.sendPush(androidPayload);
				LogUtil.info(LOGGER, "result:{}",result);
			} catch (APIConnectionException e1) {
				LogUtil.info(LOGGER, "e:{}", e1);
			} catch (APIRequestException e) {
				LogUtil.info(LOGGER, "e.getStatus:{}",  e.getStatus());
				LogUtil.info(LOGGER, "e.getErrorCode:{}", e.getErrorCode());
				LogUtil.info(LOGGER, "e.getErrorMessage:{}", e.getErrorMessage());
			}
		}
	}



	public static PushPayload AndriodbuildPushObject_all_alias_alert(Map<String,String> returnStr,String[] userIds){
		return PushPayload.newBuilder()
				.setPlatform(Platform.android())
				.setAudience(Audience.alias(userIds))
				//.setNotification(Notification.alert(returnStr.get("content")))
				.setNotification(Notification.newBuilder().addPlatformNotification(AndroidNotification.newBuilder()
						.setAlert(returnStr.get("content"))
						//.setContentAvailable(true)
						//.setSound("default")
						.addExtras(returnStr)
						//.autoBadge()
						.build()).build())
				.setOptions(Options.newBuilder().setApnsProduction(true).build())
				.build();
	}

	/**
	 * 推送自定义通知，不在通知栏显示
	 * @param userIds
	 * @param msgContent
	 */
	public MessagePush(String[] userIds,String msgContent){
		JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
		PushPayload payload= PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.alias(userIds))
				.setMessage(Message.newBuilder()
						.setMsgContent(msgContent)
						.addExtra("from", "JPush")
						.build())
				.build();
		try {
			PushResult result = jpushClient.sendPush(payload);
			LogUtil.info(LOGGER, "result:{}",result);

		} catch (APIConnectionException e1) {
			LogUtil.info(LOGGER, "e:{}", e1);

		} catch (APIRequestException e) {
			LogUtil.info(LOGGER, "e.getStatus:{}",  e.getStatus());
			LogUtil.info(LOGGER, "e.getErrorCode:{}", e.getErrorCode());
			LogUtil.info(LOGGER, "e.getErrorMessage:{}", e.getErrorMessage());
		}
	}



	public static void main(String[] args){
		String[] ars = new String[]{"17","28"};
		MessagePush messagePush = new MessagePush(new HashMap<String,String>(),ars);
	}
}
